//
//  PicturePickerViewController.swift
//  Status
//
//  Created by Kylin on 16/3/24.
//  Copyright © 2016年 Kylin. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class PicturePickerViewController: UICollectionViewController {

    // 定义属性
   lazy var images : [UIImage] = [UIImage]()
    
    // MARK:- 系统回调函数
    override func viewDidLoad() {
       super.viewDidLoad()
       
        self.collectionView?.registerNib(UINib(nibName: "PicturePickerViewCell", bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)
   }

}



// MARK:- collectionView的代理方法

extension PicturePickerViewController {
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count + 1
    }
    
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        //1:创建cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PicturePickerViewCell
        
        // 2:给cell设置数据
        cell.backgroundColor = UIColor.cyanColor()
        cell.image = indexPath.item <= images.count - 1 ? images[indexPath.item] : nil
        cell.delegate = self
        
        return cell
    }

}
// MARK:- cell 的代理方法
extension PicturePickerViewController : PicturePickerViewCellDelegate {
    func addPhotoBtnClickForCell(cell: UICollectionViewCell) {
        // 1:判断照片源是否可用
        if !UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
            return
        }
        
        // 2:创建照片选择器
        let ipc = UIImagePickerController()
        
        // 3:设置照片源
        ipc.sourceType = .PhotoLibrary
        
        // 4:设置代理
        ipc.delegate = self
        
        // 5:弹出控制器
        presentViewController(ipc, animated: true, completion: nil)
        
    }
    func removePhotoBtnClickForCell(cell: UICollectionViewCell) {
        // 1:获得删除照片的下标值
        let indexPath = collectionView?.indexPathForCell(cell)
        
        // 2:将照片从数组中删除
        images.removeAtIndex(indexPath!.item)
        
        // 3:刷新表格
        collectionView?.reloadData()
        
    }

}
// MARK:- UIImagePickerController的代理方法
extension PicturePickerViewController : UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        // 1.取出用户选择的image
        guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
            picker.dismissViewControllerAnimated(true, completion: nil)
            return
        }
        // 2.将图片放入到数组中
        images.append(image)
        
        // 3:刷新表格
        collectionView?.reloadData()
        
        // 4;退出控制器
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}

// MARK:- 自定义CollectionView的Layout
class PicturePickerCollectionViewLayout : UICollectionViewFlowLayout {
    override func prepareLayout() {
        // 1:定义间距常量
        let itemPadding : CGFloat = 15
        let itemWH = (UIScreen.mainScreen().bounds.width - 4 * itemPadding) / 3
        
        // 2:设置布局相关属性
        itemSize = CGSize(width: itemWH, height: itemWH)
        minimumInteritemSpacing = itemPadding
        minimumLineSpacing = itemPadding
        
        // 3:设置UIcollectionView相关属性
        collectionView?.contentInset = UIEdgeInsets(top: itemPadding, left: itemPadding, bottom: itemPadding, right: itemPadding)
    }
}


















